/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.asu.entity;

import com.haulmont.chile.core.annotations.Composition;
import com.haulmont.chile.core.annotations.MetaProperty;
import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.cuba.core.entity.StandardEntity;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author: Sergey42
 * Date: 30.11.13 12:35
 */
@Table(name = "EXT_DEFENSE_DAY")
@Entity(name = "ext$DefenseDay")
@NamePattern("%d|name")
public class DefenseDay extends StandardEntity {

    private static final long serialVersionUID = -3844237896126320625L;

    @ManyToOne
    @JoinColumn(name = "COMMISSION_ID")
    protected Commission commission;

    @ManyToOne
    @JoinColumn(name = "NORM_CONTROL_EMPLOYEE_ID")
    protected CathedralEmployee controller;

    @Column(name = "DEFENSE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    protected Date defenseDate;

    @OneToMany(mappedBy = "defenseDay")
    @Composition
    @OrderBy("sortOrder")
    protected List<DefenseDayStudentLink> links;

    @ManyToOne
    @JoinColumn(name = "DEFENSE_DAY_TYPE_ID")
    protected DefenseDayType defenseDayType;

    @MetaProperty
    public String getName() {
        if (defenseDayType != null && commission != null && defenseDate != null) {
            return defenseDayType.getName() + " - " + commission.getName() + " - " + new SimpleDateFormat("dd.MM.yyyy HH:mm").format(defenseDate);
        }
        return "bad view";
    }

    public Commission getCommission() {
        return commission;
    }

    public void setCommission(Commission commission) {
        this.commission = commission;
    }

    public Date getDefenseDate() {
        return defenseDate;
    }

    public void setDefenseDate(Date defenseDate) {
        this.defenseDate = defenseDate;
    }

    public List<DefenseDayStudentLink> getLinks() {
        return links;
    }

    public void setLinks(List<DefenseDayStudentLink> links) {
        this.links = links;
    }

    public DefenseDayType getDefenseDayType() {
        return defenseDayType;
    }

    public void setDefenseDayType(DefenseDayType defenseDayType) {
        this.defenseDayType = defenseDayType;
    }

    public CathedralEmployee getController() {
        return controller;
    }

    public void setController(CathedralEmployee controller) {
        this.controller = controller;
    }
}
